package com.dashu.school.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.school.dto.PageDTO;
import com.dashu.school.dto.company.CompanyParentDTO;
import com.dashu.school.dto.company.CompanyParentListDTO;
import com.dashu.school.result.PageEnty;
import com.dashu.school.result.ResultBean;
import com.dashu.school.service.CompanyParentService;
import com.dashu.school.vo.UpdateEntityVO;
import com.dashu.school.vo.company.CompanyParentListVO;
import com.dashu.school.vo.company.CompanyParentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/11/17 15:21
 */

@RequestMapping("dashu/companyParent")
@RestController
@Slf4j
@Api(value = "dashu/companyParent", tags = {"组织家长模块"}, description = "后台")
public class CompanyParentController {

    @Autowired
    private CompanyParentService companyParentService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addyParent")
    @ApiOperation(value = "家长添加", notes = "家长添加")
    public ResultBean<CompanyParentDTO> addyParent(@RequestBody @Validated CompanyParentVO companyParentVO) {
        CompanyParentDTO companyParentDTO = companyParentService.existByParent(companyParentVO.getParentPhone(), companyParentVO.getCompanyId());
        if (companyParentDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_PHONE.getCode(), ReplRyCode.EXIST_PHONE.getMessage(), null);
        }
        if (companyParentVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长添加", companyParentService.add(companyParentVO));
    }

    @PostMapping("/updataParent")
    @ApiOperation(value = "家长信息修改", notes = "家长信息修改")
    public ResultBean<CompanyParentDTO> updataPass(@RequestBody @Validated UpdateEntityVO<CompanyParentVO> updateEntityVO) throws Exception {

        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长信息修改", companyParentService.updataByCompanyParentId(updateEntityVO));

    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询家长列表", notes = "父节点查询家长列表")
    public ResultBean<List<CompanyParentDTO>> findBase(@RequestBody @Validated PageEnty<Long> creteUserVo) throws Exception {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询家长列表", companyParentService.findByCreteUser(creteUserVo));

    }

    @GetMapping("/findParent")
    @ApiOperation(value = "家长信息查询", notes = "家长信息查询")
    public ResultBean<CompanyParentDTO> findParent(long companyParentId) {
        if (companyParentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长信息查询", companyParentService.findByCompanyParentId(companyParentId));

    }

    @DeleteMapping("/deleteParent")
    @ApiOperation(value = "家长信息删除", notes = "删除家长信息")
    public ResultBean<Boolean> findUser(long companyParentId) {
        if (companyParentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "删除家长信息", companyParentService.deleteByCompanyParentId(companyParentId));

    }

    @PostMapping("/pageParentList")
    @ApiOperation(value = "家长列表", notes = "家长列表")
    public ResultBean<PageDTO<CompanyParentListDTO>> pageParentList(@RequestBody @Validated PageEnty<CompanyParentListVO> companyParentListVO) throws Exception {
        PageDTO<CompanyParentListDTO> pageDTO = new PageDTO<>();
        if (companyParentListVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyParentService.parentCount(companyParentListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyParentService.pageParentList(companyParentListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长列表", pageDTO);

    }


}
